// index.js
import {createRouter, createWebHistory} from 'vue-router'
import {ElMessage} from 'element-plus'
import {getUserInfo} from "@/utils/auth";

const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes: [
        {
            path: '/',
            component: () => import('@/views/Manager.vue'),
            redirect: '/home',
            children: [
                {path: 'person', component: () => import('@/views/manager/Person.vue')},
                {path: 'password', component: () => import('@/views/manager/Password.vue')},
                {path: 'home', component: () => import('@/views/manager/Home.vue')},
                {path: 'studyMaterial', component: () => import('@/views/studyMaterial.vue')},
                {path: 'membershipFee', component: () => import('@/views/MembershipFee.vue')},
                {path: 'relationshipTransfer', component: () => import('@/views/RelationshipTransfer.vue')},
                {
                    path: 'organizationMember',
                    component: () => import('@/views/OrganizationMember.vue'),
                    meta: {
                        requiresSecretary: false
                    }
                },
                {
                    path: 'createOrganizationBranch',
                    component: () => import('@/views/CreateOrganizationBranch.vue'),
                    meta: {
                        requiresSecretary: true
                    }
                },
                {
                    path: 'sysUser',
                    component: () => import('@/views/SysUser.vue'),
                    meta: {
                        requiresAdmin: true
                    }
                },
                {
                    path: 'organizationActivity',
                    component: () => import('@/views/OrganizationActivity.vue'),
                    meta: {
                        requiresSecretary: true // 需要书记权限
                    }
                },
                {
                    path: 'showActivities',
                    component: () => import('@/views/ShowActivities.vue'),
                    meta: {
                        requiresStudent: false // 需要学生或教师权限
                    }
                },
                {
                    path: 'myActivities',
                    component: () => import('@/views/MyActivities.vue'),
                    meta: {
                        requiresStudent: false // 需要学生或教师权限
                    }
                },
                {
                    path: 'statisticalAnalysis',
                    component: () => import('@/views/StatisticalAnalysis.vue'),
                    meta: {
                        requiresSecretary: false // 书记权限
                    }
                }
            ]
        },
        {path: '/login', component: () => import('@/views/Login.vue')},
        {path: '/register', component: () => import('@/views/Register.vue')},
    ]
})

// 添加全局路由守卫
router.beforeEach((to, from, next) => {
    const user = getUserInfo()

    if (!user && to.path !== '/login' && to.path !== '/register') {
        ElMessage.error('登录已过期，请重新登录！')
        next('/login')
        return
    }

    if (user) {
        // 检查管理员权限
        if (to.meta.requiresAdmin && user.userType !== 6) {
            ElMessage.error('权限不足，无法访问该页面！')
            next(from.path)
            return
        }

        // 检查书记权限
        if (to.meta.requiresSecretary && ![1, 2, 3].includes(user.userType)) {
            ElMessage.error('权限不足，仅党委书记和团委书记可访问！')
            next(from.path)
            return
        }

        // 检查学生/教师权限
        if (to.meta.requiresStudent && ![4, 5].includes(user.userType)) {
            ElMessage.error('权限不足，仅教师和学生可访问！')
            next(from.path)
            return
        }
    }

    next()
})

export default router
